﻿using System;
using System.Activities.DurableInstancing;
using System.Collections.Generic;
using System.Configuration;
using System.ServiceModel.Activities;
using System.ServiceModel.Activities.Activation;
using System.Xml.Linq;

namespace MvcWebRole1.Workflow
{
    public class ExpensesServiceFactory : WorkflowServiceHostFactory
    {
        protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service, Uri[] baseAddresses)
        {
            var result = base.CreateWorkflowServiceHost(service, baseAddresses);

            var connSetting = ConfigurationManager.ConnectionStrings["WorkflowInstanceStoreConnectionString"];
            var instanceStore = new SqlWorkflowInstanceStore(connSetting.ConnectionString);
            result.DurableInstancingOptions.InstanceStore = instanceStore;

            XNamespace ns = "http://tempuri.org/";
            var promoteAsVariant = new List<XName>();
            promoteAsVariant.Add(ns + "DeviceUniqueId");
            promoteAsVariant.Add(ns + "TotalAmount");

            instanceStore.Promote("ExpensesStatus", promoteAsVariant, new List<XName>());

            result.WorkflowExtensions.Add(new PromotingTrackingParticipant());

            return result;
        }
    }
}